import React from "react";
import {Route, Switch,withRouter,Redirect} from 'react-router-dom';
import routes from "./index";
import {getStore} from "@/plugins/cache";
import ArticleList from "@v/home/articleList/articleList";
// 加载路由子页面

const childRoute: React.FC = (props:any) => {
    // @ts-ignore
    const PrivateRoute = ({component: Component, ...rest}) => {
        const isLoggedIn = Boolean(getStore({name: "token"}))
        // console.log(isLoggedIn)
        return (
            <Route
                {...rest}
                render={props =>
                    isLoggedIn ? (
                        <Component {...props} />
                    ) : (
                        <Redirect to="/login"/>
                    )
                }
            />
        );
    }
    let routePath = props.match.path;// 父级路由的路径
    let routeChildren = routes.find((item: any) => {
        return item.path === routePath
    }).children;
    // console.log(routeChildren)
    return (
        <Switch>
            <Route path="/home/active">
                <div>
                    active
                </div>
            </Route>
            <Route path="/home/ArticleList">
                <ArticleList />
            </Route>
            {
                routeChildren.map((item: any) => {
                    return (
                        <PrivateRoute key={item.path} exact={item.exact} component={item.component}/>
                    )
                })
            }
            <Redirect to="/home"/>
        </Switch>
    )
}

export default withRouter(childRoute);
